<?php

namespace app\commission;

use app\models\order\OrderItem;
use app\models\product\Product;
use app\models\product\ProductAreaList;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserLevel;
use app\services\Chain;

/**
 *
 * new upgrade
 *
 * Class UpGrade
 * @package app\commission
 */
class UpGrade
{
    protected $order;
    protected $set;

    public function __construct(\app\models\order\Order $order)
    {
        $this->order = $order;

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        // 只有报单区才可以升级
        if (ProductAreaList::checkInArea($product_id, Product::$areaMap['REPORT'])) {

            // 后台设置的参数
            $this->set = SetUserUp::getLastOne();

            // 20000 升级代理
            // $total_amount = 20000;
            $total_amount = $this->set->vip1;

            if ($order->total_pay >= $total_amount) {
                $user = User::findOne(['id' => $order->user_id]);
                if ($user && $user->level < UserLevel::$keys['vip1']) {
                    $user->level = UserLevel::$keys['vip1'];
                    $user->save();
                }
            }

            // 升级关系链
            $this->chainUp($order->user_id);

        }
    }

    // 关系链 升级
    protected function chainUp($user_id)
    {
        // 包含本人
        $chain = Chain::getReferrerChain($user_id, [], true);
        $user = User::findOne(['id' => $user_id]);
        $chain = array_merge([$user], $chain);

        foreach ($chain as $u) {

            // 必须要是 代理及以上等级
            if ($u->level < UserLevel::$keys['vip1']) {
                continue;
            }

            // 直推2个代理, 成为合伙人
            if ($u->level < UserLevel::$keys['vip2']) {
                // $count = 2;
                $count = $this->set->vip2;
                $recommend = Chain::getChildren($u->id);
                $sum = 0;
                foreach ($recommend as $v) {
                    // 需要推的是代理及以上
                    if ($v['level'] >= UserLevel::$keys['vip1']) {
                        $sum += 1;
                    }
                }
                if ($sum >= $count) {
                    $u->level = UserLevel::$keys['vip2'];
                    $u->save();
                }
            }

            // 区代 | 市代
            if ($u->level < UserLevel::$keys['svip1'] || $u->level < UserLevel::$keys['svip2']) {
                // $count1 = 40;
                // $count2 = 100;
                $count1 = $this->set->svip1;
                $count2 = $this->set->svip2;
                $team = Chain::getChildrenAll($u->id, [UserLevel::$keys['vip1'],
                    UserLevel::$keys['vip2'], UserLevel::$keys['svip1'], UserLevel::$keys['svip2']]);
                $sum = count($team);

                // 团队没有包含自己
                if ($u->level < UserLevel::$keys['svip1'] && $sum >= $count1) {
                    $u->level = UserLevel::$keys['svip1'];
                    $u->save();
                }

                // 团队没有包含自己
                if ($u->level < UserLevel::$keys['svip2'] && $sum >= $count2) {
                    $u->level = UserLevel::$keys['svip2'];
                    $u->save();
                }
            }
        }
    }
}